﻿using System;
using System.Data;
using Research.FactoryDAL;
using Research.IDAL;
using Research.SQLDAL;
using System.Transactions;
using Research.Model;
using System.Collections.Generic;

namespace Research.BLL
{
    public class RprjMainBll
    {
        protected static readonly IRprjMain dal = DataAccess<RprjMain>.Create("RprjMain");

        public DataSet ShowPageRprjMain(int pageIndex, out int totalRecord)
        {
            return dal.ShowPageRprjMain(pageIndex, out totalRecord);
        }

        public bool InsertRprj(MRprjMain rprjMain)
        {
            var returnBool = false;
            using (var ts = new TransactionScope())
            {
                try
                {
                    var rPrjID = dal.InsertRprjMain(rprjMain);
                    foreach (var chargePer in rprjMain.ChargePers)
                    {
                        dal.InsertRprjChargePer(chargePer, rPrjID);
                    }

                    foreach (var dep in rprjMain.Deps)
                    {
                        dal.InsertRprjDep(dep, rPrjID);
                    }

                    foreach (var fund in rprjMain.Funds)
                    {
                        dal.InsertRprjFund(fund, rPrjID);
                    }

                    foreach (var prj in rprjMain.Prjs)
                    {
                        dal.InsertRprjPrj(prj, rPrjID);
                    }

                    foreach (var subject in rprjMain.Subjects)
                    {
                        dal.InsertRprjSubject(subject, rPrjID);
                    }

                    ts.Complete();
                    returnBool = true;
                }
                catch (Exception)
                {
                    {
                        returnBool = false;
                    }
                    throw;
                }
                finally
                {
                    ts.Dispose();
                }
            }
            return returnBool;
        }

        public bool UpdateRprj(MRprjMain rprjMain)
        {
            var returnBool = false;
            using (var ts = new TransactionScope())
            {
                try
                {
                    dal.UpdateRprjMain(rprjMain);
                    dal.DelRprjChargePer(rprjMain.RprjID);
                    dal.DelRprjDep(rprjMain.RprjID);
                    dal.DelRprjFund(rprjMain.RprjID);
                    dal.DelRprjPrj(rprjMain.RprjID);
                    dal.DelRprjSubject(rprjMain.RprjID);

                    foreach (var chargePer in rprjMain.ChargePers)
                    {
                        dal.InsertRprjChargePer(chargePer, rprjMain.RprjID);
                    }

                    foreach (var dep in rprjMain.Deps)
                    {
                        dal.InsertRprjDep(dep, rprjMain.RprjID);
                    }

                    foreach (var fund in rprjMain.Funds)
                    {
                        dal.InsertRprjFund(fund, rprjMain.RprjID);
                    }

                    foreach (var prj in rprjMain.Prjs)
                    {
                        dal.InsertRprjPrj(prj, rprjMain.RprjID);
                    }

                    foreach (var subject in rprjMain.Subjects)
                    {
                        dal.InsertRprjSubject(subject, rprjMain.RprjID);
                    }

                    ts.Complete();
                    returnBool = true;
                }
                catch (Exception)
                {
                    {
                        returnBool = false;
                    }
                    throw;
                }
                finally
                {
                    ts.Dispose();
                }
            }
            return returnBool;
        }

        public void InsertRprjPlanSchedule(MRprjPlanSchedule mRprjSchedule)
        {
            using (var ts = new TransactionScope())
            {
                try
                {
                    mRprjSchedule.AutoNum = dal.RprjAutoNumPlus(mRprjSchedule.RprjID);
                    dal.InsertRprjPlanSchedule(mRprjSchedule);
                    ts.Complete();
                }
                catch (Exception)
                {
                    {
                    }
                    throw;
                }
                finally
                {
                    ts.Dispose();
                }
            }
        }

        public bool DelRprjPlanSchedule(MRprjPlanSchedule mRprjSchedule)
        {
            if (dal.CountActualSchedule(mRprjSchedule) != 0)
                return false;
            dal.DelRprjPlanSchedule(mRprjSchedule);
            return true;

            
        }

        public void InsertRprjActualSchedule(MRprjActualSchedule actualSchedule)
        {
            dal.InsertRprjActualSchedule(actualSchedule);
        }

        public void DelRprjActualSchedule(MRprjActualSchedule actualSchedule)
         {
             dal.DelRprjActualSchedule(actualSchedule);
         }

        public MRprjPlanSchedule GetmRprjPlanScheduleByRprjIDAutoNum(int rPrjID, int autoNum)
        {
            return dal.GetmRprjPlanScheduleByRprjIDAutoNum(rPrjID, autoNum);
        }

        public void WriteUploadData(int rPrjID, int dataTypeID, string dataName, string dataPath, string uploadDate)
        {
            dal.WriteUploadData(rPrjID, dataTypeID, dataName, dataPath, uploadDate);
        }

        public void UpdateRprjPlanSchedule(MRprjPlanSchedule mRprjSchedule)
        {
            dal.UpdateRprjPlanSchedule(mRprjSchedule);
        }

    }

    public class BasicBll
    {
        protected static readonly IBasic dal = DataAccess<Basic>.Create("Basic");

        public List<MRprjChargePer> GetChargePerList(string strWhere)
        {
            return dal.GetChargePerList("%" + strWhere + "%", "top 10");
        }

        public DataSet GetSubjectList()
        {
            return dal.GetSubjectList();
        }

        public DataSet GetDepList()
        {
            return dal.GetDepList();
        }

        public DataSet GetPlatformList()
        {
            return dal.GetPlatformList();
        }

        public DataSet GetRprjRoleList()
        {
            return dal.GetRprjRoleList();
        }

        public DataSet GetFundList()
        {
            return dal.GetFundList();
        }

        public List<MRprjPrj> GetPrjList(string strWhere)
        {
            return dal.GetPrjList(strWhere);
        }

        public string GetPrjName(string strWhere)
        {
            return dal.GetPrjName(strWhere);
        }

        public List<MRprjPlanSchedule> GetPlanScheduleList(int rPrjID)
        {
            return dal.GetPlanScheduleList(rPrjID);
        }
    
        public List<MRprjActualSchedule> GetActualScheduleList(MRprjPlanSchedule mPlanSchedule)
        {
            return dal.GetActualScheduleList(mPlanSchedule);
        }
        
        public MRprjMain GetRprjMainById(int rPrjID)
        {
            return dal.GetRprjMainById(rPrjID);
        }

        public DataSet GetRprjData(int rPrjID, int dataTypeID)
        {
            return dal.GetRprjData(rPrjID, dataTypeID);
        }
    }
}
